<template>
  <view class="content">
    <ol>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
      <li>
        云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动云税互动
      </li>
    </ol>
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: "touchScroll",
      params: { touchstart: 0, startTop: 0, touchmove: 0, endTop: 0 },
    };
  },
  onLoad() {
    // 滚动体、触发区域和滚动方向x或y
    this.$nextTick(() => {
      this.touchScroll(".content", ".content", "x");
    });
  },
  methods: {
    // 滚动主体  触发区域
    touchScroll(scrollBody, touchArea, direction) {
      let that = this;
      // 滚动主体
      let scroll_body = document.querySelector(`${scrollBody}`);
      //触发区域
      let touch_area = document.querySelector(`${touchArea}`);
      let startTop = { x: 0, y: 0 }; // 滚动的起点
      let touchstart = { x: 0, y: 0 }; // 触摸的起点
      touch_area.addEventListener("touchstart", function (event) {
        let translateData = scroll_body.style.transform.match(/-?\d+/g);
        // 获取上一次滚动的位置
        startTop.x = Number(translateData ? translateData[0] : 0);
        startTop.y = Number(translateData ? translateData[1] : 0);
        // 获取触摸的起点位置
        touchstart = event.changedTouches[0].pageY;
        let touchstart = {
          x: event.changedTouches[0].pageX,
          y: event.changedTouches[0].pageY,
        };
        touch_area.addEventListener(
          "touchmove",
          function (event) {
            event.preventDefault();
            let maxHeight =
              scroll_body.parentElement.offsetHeight - scroll_body.offsetHeight;
            let maxWidth =
              scroll_body.parentElement.offsetWidth - scroll_body.offsetWidth;
            let touchmove = {
              x: event.changedTouches[0].pageX - touchstart.x,
              y: event.changedTouches[0].pageY - touchstart.y,
            };

            // 重新定位需要的目标top值
            let endTop = {
              x: startTop.x + touchmove.x,
              y: startTop.y + touchmove.y,
            };
            if (direction == "x") {
              if (endTop.x < 0 && endTop.x > maxWidth) {
                scroll_body.style.transform = `translate(${endTop.x}px,0px)`;
                return;
                // 10 ~ max
              } else if (endTop.x >= 0) {
                scroll_body.style.transform = "translate(0px,0px)";
                return;
              } else if (endTop.x < maxWidth) {
                scroll_body.style.transform = `translate(${maxWidth}px,0px)`;
                return;
              }
            } else {
              if (endTop.y < 0 && endTop.y > maxHeight) {
                scroll_body.style.transform = `translate(0px,${endTop.y}px)`;
                return;
                // 10 ~ max
              } else if (endTop.y >= 0) {
                scroll_body.style.transform = "translate(0px,0px)";
                return;
              } else if (endTop.y < maxHeight) {
                scroll_body.style.transform = `translate(0px,${maxHeight}px)`;
                return;
              }
            }
          },
          { passive: false }
        );
      });
    },
  },
};
</script>

<style>
page {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}
.content {
  width: 600px;
  border: 1px solid pink;
  padding-left: 20px;
}
ol > li:nth-child(4n-3) {
  color: red;
}
</style>
